package com.cnbot.toolkit.log.zlog;

import android.util.Log;

import java.util.concurrent.LinkedBlockingQueue;

/**
 *  @项目名：  Test
 *  @包名：    com.cnbot.test.zlog
 *  @文件名:   LogDispatcherThread
 *  @创建者:   ww
 *  @创建时间:  2019/5/29 14:22
 *  @描述：    日志写入线程
 */
public class LogDispatchRunnable implements Runnable {

	private static final String TAG = "LogDispatchRunnable";
	/**
	 * 日志是否写入文件的标志
	 * true：支持写入 false：不支持，暂时将日志缓存到队列中
	 * @see LogQueue#add(LogBean)
	 */
	private volatile boolean mSaveToFileTag = true;
	private LinkedBlockingQueue<LogBean> mLogQueue;

	LogDispatchRunnable(LinkedBlockingQueue<LogBean> logQueue, ILogFileStrategy strategy) {
		this.mLogQueue = logQueue;
		Util.setLogFileStrategy(strategy);
	}

	public LogDispatchRunnable(LinkedBlockingQueue<LogBean> blockingQueue) {
		this(blockingQueue, new DefaultLogFileStrategy());
	}

	@Override
	public void run() {

		try {
			if (!mSaveToFileTag) {
				Log.e(TAG, "暂停写入日志文件");
				return;
			}
			LogBean logBean = mLogQueue.take();
//			Log.e(TAG, logBean.toString());
			Util.saveTextToFile(logBean.getLogText());
		} catch (InterruptedException var2) {
			Log.e(TAG, var2.toString());
		}
	}

	/**
	 * 是否允许日志文件写入
	 * @param allowed
	 */
	public void allowSave(boolean allowed) {
		mSaveToFileTag = allowed;
	}

}
